package com.sec.foodit;

import java.util.Timer;
import java.util.TimerTask;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;

import com.nhn.android.maps.NMapActivity;
import com.nhn.android.maps.NMapCompassManager;
import com.nhn.android.maps.NMapController;
import com.nhn.android.maps.NMapLocationManager;
import com.nhn.android.maps.NMapView;
import com.nhn.android.maps.maplib.NGeoPoint;
import com.nhn.android.maps.overlay.NMapPOIitem;
import com.nhn.android.mapviewer.overlay.NMapMyLocationOverlay;
import com.nhn.android.mapviewer.overlay.NMapOverlayManager;
import com.nhn.android.mapviewer.overlay.NMapPOIdataOverlay;

public class MapActivity extends NMapActivity {
	public ProgressDialog pd;
	
	
	private ActivityManager am = ActivityManager.getInstance();
	
	private static final String API_KEY = "532d059f983dce94d2e162dc176630c4";

	private NMapView mMapView;
	private NMapController mMapController;

	private static final NGeoPoint NMAP_LOCATION_DEFAULT = new NGeoPoint(127.032271, 37.4896091);
	private static final int NMAP_ZOOMLEVEL_DEFAULT = 13;
	private static final int NMAP_VIEW_MODE_DEFAULT = NMapView.VIEW_MODE_VECTOR;
	private static final boolean NMAP_TRAFFIC_MODE_DEFAULT = false;
	private static final boolean NMAP_BICYCLE_MODE_DEFAULT = false;

	private static final String KEY_ZOOM_LEVEL = "NMapViewer.zoomLevel";
	private static final String KEY_CENTER_LONGITUDE = "NMapViewer.centerLongitudeE6";
	private static final String KEY_CENTER_LATITUDE = "NMapViewer.centerLatitudeE6";
	private static final String KEY_VIEW_MODE = "NMapViewer.viewMode";
	private static final String KEY_TRAFFIC_MODE = "NMapViewer.trafficMode";
	private static final String KEY_BICYCLE_MODE = "NMapViewer.bicycleMode";

	private SharedPreferences mPreferences;

	private NMapOverlayManager mOverlayManager;

	private NMapMyLocationOverlay mMyLocationOverlay;
	private NMapLocationManager mMapLocationManager;
	private NMapCompassManager mMapCompassManager;
	
	
	private int longitudeE6 = NMAP_LOCATION_DEFAULT.getLongitudeE6();
	private int latitudeE6 = NMAP_LOCATION_DEFAULT.getLatitudeE6();
	private int level = NMAP_ZOOMLEVEL_DEFAULT;
	private int viewMode = NMAP_VIEW_MODE_DEFAULT;
	private boolean trafficMode = NMAP_TRAFFIC_MODE_DEFAULT;
	private boolean bicycleMode = NMAP_BICYCLE_MODE_DEFAULT;

	//private NMapViewerResourceProvider mMapViewerResourceProvider;

	private NMapPOIdataOverlay mFloatingPOIdataOverlay;
	private NMapPOIitem mFloatingPOIitem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		
		am.addActivity(this);
		getActionBar().hide();
		
		// initialize map view
		mMapView = (NMapView)findViewById(R.id.mapView);
		mMapView.setApiKey(API_KEY);
		mMapView.setClickable(true);
		mMapView.setEnabled(true);
		mMapView.setFocusable(true);
		mMapView.setFocusableInTouchMode(true);
		mMapView.requestFocus();

		// register listener for map state changes
		//mMapView.setOnMapStateChangeListener(onMapViewStateChangeListener);
		//mMapView.setOnMapViewTouchEventListener(onMapViewTouchEventListener);
		//mMapView.setOnMapViewDelegate(onMapViewTouchDelegate);

		// use map controller to zoom in/out, pan and set map center, zoom level etc.
		mMapController = mMapView.getMapController();

		// use built in zoom controls
		NMapView.LayoutParams lp = new NMapView.LayoutParams(LayoutParams.WRAP_CONTENT,
			LayoutParams.WRAP_CONTENT, NMapView.LayoutParams.BOTTOM_RIGHT);
		mMapView.setBuiltInZoomControls(true, lp);

		// create resource provider
		//mMapViewerResourceProvider = new NMapViewerResourceProvider(this);

		// set data provider listener
		//super.setMapDataProviderListener(onDataProviderListener);

		// create overlay manager
		//mOverlayManager = new NMapOverlayManager(this, mMapView, mMapViewerResourceProvider);
		// register callout overlay listener to customize it.
		//mOverlayManager.setOnCalloutOverlayListener(onCalloutOverlayListener);
		// register callout overlay view listener to customize it.
		//mOverlayManager.setOnCalloutOverlayViewListener(onCalloutOverlayViewListener);

		// location manager
		//mMapLocationManager = new NMapLocationManager(this);
		//mMapLocationManager.setOnLocationChangeListener(onMyLocationChangeListener);

		// compass manager
		//mMapCompassManager = new NMapCompassManager(this);

		// create my location overlay
		//mMyLocationOverlay = mOverlayManager.createMyLocationOverlay(mMapLocationManager, mMapCompassManager);

		mMapController.setMapViewMode(viewMode);
		mMapController.setMapViewTrafficMode(trafficMode);
		mMapController.setMapViewBicycleMode(bicycleMode);
		mMapController.setMapCenter(new NGeoPoint(longitudeE6, latitudeE6), level);
		
		
		//tapaha
		Button btnSearch = (Button)findViewById(R.id.btnSearch);
		
		btnSearch.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				pd = ProgressDialog.show(MapActivity.this, "Searching..", "Foodit is searching restaurants");
            	pd.setCancelable(false);
            	
            	Timer t = new Timer();
            	t.schedule(new TimerTask() {
					
					@Override
					public void run() {
						pd.dismiss();
						startActivity(new Intent("com.sec.foodit.HomeScreenActivity"));
	                	overridePendingTransition(0, 0);
					}
				}, 3000);
            	
				//startActivity(new Intent("com.sec.foodit.HomeScreenActivity"));
				//startActivity(new Intent("com.sec.foodit.TutorialActivity"));
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_map, menu);
		//startActivity(new Intent("com.sec.foodit.PostFormActivity"));
		return true;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		am.removeActivity(this);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			AlertDialog dialog = null;
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Foodit을 종료하시겠습니까?");
			//builder.setMessage("Foodit을 종료하시겠습니까?");
			builder.setPositiveButton("종료", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					am.finishAllActivity();
				}
			});
			builder.setNegativeButton("취소", null);
			builder.show();
			
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

}
